<script setup>
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { getOverviewApi } from '@/api/report'
import { ElMessage } from 'element-plus'

const router = useRouter()

// 概览数据
const overviewData = ref({
  clueTotal: 0,
  clueWaitAllot: 0,
  clueWaitFollow: 0,
  clueFollowing: 0,
  clueFalse: 0,
  clueConvertBusiness: 0,
  businessTotal: 0,
  businessWaitAllot: 0,
  businessWaitFollow: 0,
  businessFollowing: 0,
  businessFalse: 0,
  businessConvertCustomer: 0
})

// 加载状态
const loading = ref(false)

// 获取概览数据
const getOverviewData = async () => {
  loading.value = true
  try {
    const result = await getOverviewApi()
    if (result.code === 1) {
      overviewData.value = result.data
    } else {
      ElMessage.error(result.msg || '获取概览数据失败')
    }
  } catch (error) {
    console.error('获取概览数据失败:', error)
    ElMessage.error('获取概览数据失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 快捷操作跳转
const handleQuickAction = (path) => {
  router.push(path)
}

// 页面刷新
onMounted(() => {
  getOverviewData()
})
</script>

<template>
  <div class="home-page">
    <!-- 页面标题 -->
    <div class="page-title">
      <h1>欢迎您, 进入轻客管家系统</h1>
    </div>

    <!-- 概览数据区域 -->
    <div class="overview-section" v-loading="loading">
      <el-row :gutter="20">
        <!-- 线索概览 -->
        <el-col :span="12">
          <div class="overview-card">
            <div class="card-title">线索概览</div>
            <div class="data-grid">
              <div class="data-item">
                <div class="data-label">总线索数</div>
                <div class="data-value">{{ overviewData.clueTotal }}</div>
              </div>
              <div class="data-item">
                <div class="data-label">待分配线索数</div>
                <div class="data-value">{{ overviewData.clueWaitAllot }}</div>
              </div>
              <div class="data-item">
                <div class="data-label">待跟进线索数</div>
                <div class="data-value">{{ overviewData.clueWaitFollow }}</div>
              </div>
              <div class="data-item">
                <div class="data-label">跟进中线索数</div>
                <div class="data-value">{{ overviewData.clueFollowing }}</div>
              </div>
              <div class="data-item">
                <div class="data-label">伪线索数</div>
                <div class="data-value">{{ overviewData.clueFalse }}</div>
              </div>
              <div class="data-item">
                <div class="data-label">转商机线索数</div>
                <div class="data-value">{{ overviewData.clueConvertBusiness }}</div>
              </div>
            </div>
          </div>
        </el-col>

        <!-- 商机概览 -->
        <el-col :span="12">
          <div class="overview-card">
            <div class="card-title">商机概览</div>
            <div class="data-grid">
              <div class="data-item">
                <div class="data-label">总商机数</div>
                <div class="data-value">{{ overviewData.businessTotal }}</div>
              </div>
              <div class="data-item">
                <div class="data-label">待分配商机数</div>
                <div class="data-value">{{ overviewData.businessWaitAllot }}</div>
              </div>
              <div class="data-item">
                <div class="data-label">待跟进商机数</div>
                <div class="data-value">{{ overviewData.businessWaitFollow }}</div>
              </div>
              <div class="data-item">
                <div class="data-label">跟进中商机数</div>
                <div class="data-value">{{ overviewData.businessFollowing }}</div>
              </div>
              <div class="data-item">
                <div class="data-label">回收商机数</div>
                <div class="data-value">{{ overviewData.businessFalse }}</div>
              </div>
              <div class="data-item">
                <div class="data-label">转客户商机数</div>
                <div class="data-value">{{ overviewData.businessConvertCustomer }}</div>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- 快捷操作区域 -->
    <div class="quick-actions">
      <el-row :gutter="20">
        <el-col :span="4">
          <div class="action-card" @click="handleQuickAction('/resource/course')">
            <div class="action-icon">
              <el-icon><Document /></el-icon>
            </div>
            <div class="action-title">课程管理</div>
            <div class="action-subtitle">课程信息维护</div>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="action-card" @click="handleQuickAction('/resource/activity')">
            <div class="action-icon">
              <el-icon><Calendar /></el-icon>
            </div>
            <div class="action-title">活动管理</div>
            <div class="action-subtitle">营销活动管理</div>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="action-card" @click="handleQuickAction('/clue/list')">
            <div class="action-icon">
              <el-icon><User /></el-icon>
            </div>
            <div class="action-title">线索管理</div>
            <div class="action-subtitle">潜在客户追踪</div>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="action-card" @click="handleQuickAction('/business/list')">
            <div class="action-icon">
              <el-icon><TrendCharts /></el-icon>
            </div>
            <div class="action-title">商机管理</div>
            <div class="action-subtitle">销售机会管理</div>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="action-card" @click="handleQuickAction('/customer')">
            <div class="action-icon">
              <el-icon><Avatar /></el-icon>
            </div>
            <div class="action-title">客户管理</div>
            <div class="action-subtitle">客户信息维护</div>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="action-card" @click="handleQuickAction('/system/log')">
            <div class="action-icon">
              <el-icon><Histogram /></el-icon>
            </div>
            <div class="action-title">日志统计</div>
            <div class="action-subtitle">系统操作日志</div>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<style scoped>
.home-page {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.page-title {
  text-align: center;
  margin-bottom: 30px;
}

.page-title h1 {
  font-size: 50px;
  color: #333;
  font-weight: 500;
  margin: 0;
  font-family: "楷体", "KaiTi", serif;
}

.overview-section {
  margin-bottom: 40px;
}

.overview-card {
  background-color: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  height: 280px;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 20px;
  text-align: center;
}

.data-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 12px;
  height: calc(100% - 50px);
}

.data-item {
  background-color: #f8f9fa;
  border-radius: 6px;
  padding: 12px;
  text-align: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
  transition: all 0.3s ease;
}

.data-item:hover {
  background-color: #e9ecef;
  transform: translateY(-2px);
}

.data-label {
  font-size: 11px;
  color: #666;
  margin-bottom: 6px;
}

.data-value {
  font-size: 22px;
  font-weight: 600;
  color: #409eff;
}

.quick-actions {
  margin-top: 40px;
}

.action-card {
  background-color: #fff;
  border-radius: 8px;
  padding: 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  height: 100px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.action-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.action-icon {
  font-size: 28px;
  color: #409eff;
  margin-bottom: 8px;
}

.action-title {
  font-size: 15px;
  font-weight: 600;
  color: #333;
  margin-bottom: 2px;
}

.action-subtitle {
  font-size: 11px;
  color: #666;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .data-grid {
    grid-template-columns: 1fr;
    grid-template-rows: repeat(6, 1fr);
  }
  
  .overview-card {
    height: auto;
    min-height: 400px;
  }
}

@media (max-width: 768px) {
  .home-page {
    padding: 10px;
  }
  
  .page-title h1 {
    font-size: 20px;
  }
  
  .action-card {
    height: 100px;
    padding: 16px;
  }
  
  .action-icon {
    font-size: 24px;
  }
  
  .action-title {
    font-size: 14px;
  }
  
  .action-subtitle {
    font-size: 10px;
  }
}
</style>
